#!/bin/bash
# $1 = windows ip

if [ $# != 1 ] ; then 
    echo "USAGE: $0 WindowsIP"
    exit 1; 
fi

# 过滤出133的IP
addr=`/sbin/ifconfig|grep inet|grep -v 127.0.0.1|grep -v inet6|grep '133'|awk '{print $2}'|tr -d "addr:"`

if  [ ! -n "$addr" ] ;then
    echo "please modeify the shell to grep your localIp addr"
    exit
fi

localIp=(
    'xcheck/record-replay/public/js/record-replay-0.2/remote-instrumenter.js'
    'xcheck/record-replay/public/js/record-replay-0.2/remote-record-replay.js'
    'xcheck/record-replay/public/js/record-replay-0.3/remote-instrumenter.js'
    'xcheck/record-replay/public/js/record-replay-0.3/remote-record-replay.js'
    'xcheck/record-replay/public/js/record-replay-1.0/remote-instrumenter.js'
    'xcheck/record-replay/public/js/record-replay-1.0/remote-record-replay.js'
    'xcheck-web/DbConn.js'
    'xcheck-web/properties.js'
    'xcheck-proxy/src/main/resource/edu/ubc/remotejs/config/remote.js.properties'
    'xcheck-proxy/src/main/resource/edu/ubc/remotejs/config/remote.js.properties'
    'xcheck-proxy/config/WebScarab.properties'
)

# 修改本地IP
for str in ${localIp[*]}
do
    # record-replay
    sed -i "" "s/serverip = \".*\"/serverip = \"${addr}\"/g" $str

    # DbConn.js
    sed -i "" "s/mongodb:\/\/.*:27017/mongodb:\/\/${addr}:27017/g" $str

    # properties.js
    sed -i "" "s/databaseIp = \".*\"/databaseIp = \"${addr}\"/g" $str
    sed -i "" "s/proxyHost = \".*\"/proxyHost = \"${addr}\"/g" $str

    # remote.js.properties
    sed -i "" "s/server.ip=.*/server.ip=${addr}/g" $str

    # WebScarab.properties
    sed -i "" "s/Proxy.listeners=.*:/Proxy.listeners=${addr}:/g" $str

done

winIp=(
    'xcheck-web/myutils.js'
)

# 修改windows IP
for str in ${winIp[*]}
do
    sed -i "" "s/http:\/\/.*:8080/http:\/\/$1:8080/g" $str
done

mongod

# cd xcheck-web
# node server.js

# cd ../xcheck/record-replay
# node index.js

